﻿/**
* @description  Takes multiple tweens and runs them in a sequence.
*/
package com.foed.animation {

	import com.foed.events.TweenEvent;

	public class TweenSequence extends Animator {
	
		private var _tween:Animator;
		private var _tweens:Array;
	
		/**
		* @description  Constructor.
		*
		* @param  pTweens  A list of tweens to run, in the order to be run.
		*/
		public function TweenSequence(pTweens:Array) {
			_tweens = pTweens;
		}
	
		/**
		* @description  Called each time a single tween completes, kicking off the next tween.
		*
		* @param  pEvent  Event fired by Animator.
		*/
		private function onEndTween(pEvent:TweenEvent):void {
			_tween.removeEventListener(TweenEvent.END, onEndTween);
			runTween();
		}
	
		/**
		* @description  Starts off next tween in sequence.
		*/
		private function runTween():void {
			_tween = (_tweens.shift() as Animator);
			if (_tween == null) {
				dispatchEvent(new TweenEvent(TweenEvent.END, this));
			} else {
				_tween.addEventListener(TweenEvent.END, onEndTween);
				_tween.start();
			}
		}
	
		/**
		* @description  Stops the animation.
		*/
		override public function halt():void {
			_tween.removeEventListener(TweenEvent.END, onEndTween);
			_tween.halt();
		}
	
		/**
		* @description  Starts the animation.
		*/
		override public function start():void {
			runTween();
		}
	
		/**
		* @description  Cleans up any timers that might be running.
		*/
		override public function die():void {
			super.die();
			_tweens = null;
		}
		
	}
	
}